package com.controller;

import com.POJO.Subject;
import com.POJO.TeacherSubjectClass;
import com.service.SubjectService;
import com.service.TeacherSubjectClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Package com.controller
 * @Date 2024-05-23 12:07
 * @Description 科目管理的控制器类，处理前端关于科目的请求
 */
@Controller
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    @Autowired
    private TeacherSubjectClassService teacherSubjectClassService; // 注入服务类

    /**
     * 获取教师的所有授课科目和班级
     *
     * @param teacherId 教师ID
     * @return 包含教师授课科目和班级的视图
     */
    @GetMapping("/teacher/{teacherId}")
    public ModelAndView getSubjectsAndClassesByTeacherId(@PathVariable("teacherId") int teacherId) {
        List<Subject> subjects = subjectService.getSubjectsTeacherId(teacherId);
        List<TeacherSubjectClass> teacherSubjectClasses = teacherSubjectClassService.getTeacherSubjectClassByTeacherId(teacherId);
        ModelAndView modelAndView = new ModelAndView("teacherSubjects");
        modelAndView.addObject("subjects", subjects);
        modelAndView.addObject("teacherSubjectClasses", teacherSubjectClasses);
        modelAndView.addObject("teacherId", teacherId);
        return modelAndView;
    }


    /**
     * 跳转到添加或更新科目视图
     *
     * @param teacherId 教师ID
     * @return 添加或更新科目视图
     */
//    @GetMapping("/{teacherId}/subjects/new")
//    public ModelAndView addOrUpdateSubject(@PathVariable("teacherId") int teacherId) {
//        TeacherSubjectClass teacherSubjectClass = teacherSubjectClassService.getTeacherSubjectClassByTeacherId(teacherId).get(0); // 假设只取一个
//        ModelAndView mv = new ModelAndView("teacherSubjects");
//        mv.addObject("teacher", teacherId);
//        mv.addObject("subject", new Subject());
//        mv.addObject("classes", getTeacherById(teacherId));
//        return mv;
//    }


    /**
     * 新增授课科目
     *
     * @param subject 科目对象
     * @return 重定向到教师授课科目视图
     */
    @PostMapping("/add")
    public ModelAndView insertSubject(@ModelAttribute("subject") Subject subject) {
        subjectService.insertSubject(subject);
        ModelAndView modelAndView = new ModelAndView("redirect:/subject/teacher/" + subject.getTeacherId());
        modelAndView.addObject("message", "Subject added successfully!");
        return modelAndView;
    }

    /**
     * 修改科目
     *
     * @param subjectId      科目ID
     * @param updatedSubject 更新后的科目对象
     * @return 重定向到教师授课科目视图
     */
    @PutMapping("/{subjectId}")
    public ModelAndView updateSubject(@PathVariable int subjectId, @ModelAttribute Subject updatedSubject) {
        updatedSubject.setSubjectId(subjectId);
        subjectService.updateSubject(updatedSubject);
        ModelAndView modelAndView = new ModelAndView("redirect:/subject/teacher/" + updatedSubject.getTeacherId());
        modelAndView.addObject("message", "Subject updated successfully");
        return modelAndView;
    }

    /**
     * 删除授课科目
     *
     * @param subjectId 科目ID
     * @return 重定向到教师授课科目视图
     */

    @RequestMapping("/subjects/delete/{subjectId}/{teacherId}")
    public ModelAndView deleteSubject(@PathVariable("subjectId") int subjectId, @PathVariable("teacherId") int teacherId) {
        subjectService.deleteSubject(subjectId);
        ModelAndView modelAndView = new ModelAndView("redirect:/subject/teacher/" + teacherId);
        modelAndView.addObject("message", "Subject deleted successfully!");
        return modelAndView;
    }

    /**
     * 新增授课班级
     *
     * @param teacherId 教师ID
     * @param classId   班级ID
     * @param subjectId 科目ID
     * @return 重定向到教师授课科目视图
     */
    @PostMapping("/teachers/{teacherId}/add")
    public ModelAndView insertClass(@PathVariable("teacherId") int teacherId, @RequestParam("classId") int classId, @RequestParam("subjectId") int subjectId) {
        TeacherSubjectClass teacherSubjectClass = new TeacherSubjectClass();
        teacherSubjectClass.setTeacherId(teacherId);
        teacherSubjectClass.setClassId(classId);
        teacherSubjectClass.setSubjectId(subjectId);
        teacherSubjectClassService.insertTeacherSubjectClass(teacherSubjectClass);
        ModelAndView modelAndView = new ModelAndView("redirect:/subject/teacher/" + teacherId);
        modelAndView.addObject("message", "Class added successfully");
        return modelAndView;
    }

    /**
     * 删除授课班级
     *
     * @param teacherId 教师ID
     * @param classId   班级ID
     * @return 重定向到教师授课科目视图
     */
    @PostMapping("/teachers/{teacherId}/delete/{classId}")
    public ModelAndView deleteClass(@PathVariable int teacherId, @PathVariable int classId) {
        teacherSubjectClassService.deleteTeacherSubjectClass(classId);
        ModelAndView modelAndView = new ModelAndView("redirect:/subject/teacher/" + teacherId);
        modelAndView.addObject("message", "Class deleted successfully");
        return modelAndView;
    }


    /**
     * 跳转到教师授课科目视图
     *
     * @return teacherSubjects视图
     */
    @GetMapping("/")
    public String toTeacherSubjects() {
        return "teacherSubjects";
    }
}
